<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Db\Db;
use Imi\Util\Random;
use Imi\Bean\BeanFactory;
use Imi\Util\ObjectArrayHelper;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Team;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use ImiDemo\HttpDemo\MainServer\Model\UserSns;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;

/**
 * Model操作 演示
 * @Controller("/ModelPolymorphicO2O/")
 * @View(renderType="json")
 */
class ModelPolymorphicO2OController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/findUser
     * 
     * @Action
     * @return void
     */
    public function findUser()
    {
        $user = User::find(1);
        $user->avatar->queryRelations('relationModel');
        return [
            'user'            =>    $user,
            'avatarClass'    =>    BeanFactory::getObjectClass($user->avatar->relationModel),
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/findTeam
     * 
     * @Action
     * @return void
     */
    public function findTeam()
    {
        $team = Team::find(1);
        $team->avatar->queryRelations('relationModel');
        return [
            'team'            =>    $team,
            'avatarClass'    =>    BeanFactory::getObjectClass($team->avatar->relationModel),
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/findUser2
     * 
     * @Action
     * @return void
     */
    public function findUser2()
    {
        $user = User::find(1);
        $user->avatar->queryRelations('user');
        return [
            'user'            =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/select
     * 
     * @Action
     * @return void
     */
    public function select()
    {
        $list = User::select();

        return $list;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/newInstance
     * 
     * @Action
     * @return void
     */
    public function newInstance()
    {
        $user = User::newInstance();

        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/insert
     * 
     * @Action
     * @return void
     */
    public function insert()
    {
        $user = UserWithFriend::newInstance();
        $user->username = Random::letterAndNumber(6, 16);
        $user->ex->intro = '这个人很懒，什么也没留下';
        $user->avatar->url = 'http://www.baidu.com/' . mt_rand() . '.jpg';
        $result = $user->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $user = UserWithFriend::find(1);
        $user->ex->intro = '这个人很懒，什么也没留下-' . date('Y-m-d H:i:s');
        $user->avatar->url = 'http://www.baidu.com/update.jpg';

        $result = $user->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicO2O/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = UserWithFriend::query()->max('id');
        $user = UserWithFriend::find($maxId);
        $result = $user->delete();

        return [
            'success'    =>    $result->isSuccess(),
        ];
    }
}